package com.jk.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import com.jk.mapper.ProjectMapper;
import com.jk.pojo.Project;
import com.jk.service.ProjectService;

@Service
@Transactional
public class ProjectServiceImpl extends BaseServiceImpl<Project> implements ProjectService {

	@Resource
	private ProjectMapper projectMapper;

	@Transactional(readOnly=true)
	@Override
	public List<Project> queryList(String projectName, String firstTimeStr,String endTimeStr) throws Exception {
		Example example = new Example(Project.class);
    	Criteria criteria = example.createCriteria();
    	if(StringUtils.isNotEmpty(projectName)){
    		criteria.andLike("projectName", "%"+projectName+"%");
    	}if(StringUtils.isNotEmpty(firstTimeStr) && StringUtils.isNotEmpty(endTimeStr)){
    		Date firstTime = DateUtils.parseDate(firstTimeStr+" 00:00:00", new String[]{"yyyy-MM-dd HH:mm:ss"});
    		Date endTime = DateUtils.parseDate(endTimeStr+" 23:59:59", new String[]{"yyyy-MM-dd HH:mm:ss"});
    		criteria.andBetween("applyTime", firstTime, endTime);
    	}
		return this.selectByExample(example);
	}

	@Override
	@Transactional(readOnly=true)
	public List<Project> queryProjectDelievery(HttpServletRequest request, ModelMap modelMap, Long userId) {
		return projectMapper.queryProjectDelievery(userId);
	}

	@Transactional(readOnly=true)
	@Override
	public List<Project> findProjectList() throws Exception {
		return projectMapper.findProjectList();
	}

}
